package com.sinaapp.gavinzhang.tongbu;

/**
 * Created by gavin on 15-8-14.
 */
public class PrintABC implements Runnable{
    private Object prev;
    private Object self;
    private String name;

    private int count = 10;

    public PrintABC(Object prev, Object self, String name) {
        this.prev = prev;
        this.self = self;
        this.name = name;
    }


    public void run() {
        while (count-- != 0){
            synchronized(prev){
                synchronized (self){
                    System.out.print(name);
                    self.notify();
                }
                try {
                    prev.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void start(){
        Thread thread = new Thread(this);
        thread.start();
    }

    public static void main(String[] args){
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();

        PrintABC pa = new PrintABC(c,a,"A");
        PrintABC pb = new PrintABC(a,b,"B");
        PrintABC pc = new PrintABC(b,c,"C");

        pa.start();
        pb.start();
        pc.start();

    }
}
